<template>
  <div
    :class="{'text-left':(position=='left'),'text-center':(position=='center'),'text-right':(position=='right')}">
    <div class="pagination-box">
      <div class="icon-arrow" :class="{'disabled':(currentPage==1)}"
           @click="tipPrevPage">
        <i class="el-icon-arrow-left"></i>
      </div>
      <div class="pagination-pager-div">
        <span class="current-page">{{currentPage}}</span>
        /
        <span class="total-page">{{totalPages}}</span>
      </div>
      <div class="icon-arrow" :class="{'disabled':(currentPage==totalPages)}"
           @click="tipNextPage">
        <i class="el-icon-arrow-right"></i>
      </div>
      <div class="pagination-input-div">
        前往
        <el-input class="pagination-input" v-model="curPage" placeholder=" "
                  @change="changeInputPage"></el-input>
        页
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "simple-pagination",
    props: {
      'position': {
        type: String,
        default() {
          return 'left';
        }
      },
      'currentPage': {
        type: Number,
        default() {
          return 1;
        }
      },
      'pageSize': {
        type: Number,
        default() {
          return 10;
        }
      },
      'total': {
        type: Number,
        default() {
          return 0;
        }
      },
    },
    data() {
      return {
        curPage: 1,
      }
    },
    watch: {
      currentPage: {
        handler(newVal, oldVal) {
          this.curPage = newVal || 1;
        },
        immediate: true,
        deep: true,
      },
    },
    computed: {
      totalPages() {
        return Math.ceil(this.total / this.pageSize);
      },
    },
    created() {
      this.curPage = this.currentPage;
    },
    methods: {
      changeInputPage(page_num) {
        if (page_num >= this.totalPages) {
          page_num = this.totalPages;
          this.curPage = page_num;
        }
        this.$emit('currentChange', page_num);
      },
      tipPrevPage() {
        if (this.currentPage == 1) {
          return;
        }
        let page_num = this.currentPage - 1;
        this.$emit('currentChange', page_num);
      },
      tipNextPage() {
        if (this.currentPage == this.totalPages) {
          return;
        }
        let page_num = this.currentPage + 1;
        this.$emit('currentChange', page_num);
      },
    }
  }
</script>

<style scoped>
  .text-center {
    text-align: center;
    text-align: -moz-center;
    text-align: -webkit-center;
  }

  .text-left {
    text-align: left;
    text-align: -moz-left;
    text-align: -webkit-left;
  }

  .text-right {
    text-align: right;
    text-align: -moz-right;
    text-align: -webkit-right;
  }

  .pagination-box {
    display: inline-block;
    color: #FFFFFF;
    overflow: hidden;
    min-height: 29px;
    line-height: 29px;
  }

  .pagination-box .icon-arrow,
  .pagination-box .pagination-pager-div,
  .pagination-box .pagination-input-div {
    float: left;
  }

  .pagination-box .icon-arrow.disabled {
    color: rgba(255, 255, 255, 0.4);
  }

  .pagination-box .current-page,
  .pagination-box .total-page {
    width: auto;
    min-width: 1em;
  }

  .pagination-box .icon-arrow {
    width: 27px;
  }

  .pagination-box .icon-arrow,
  .pagination-box .current-page,
  .pagination-box .total-page,
  .pagination-input-div .pagination-input,
  /deep/ .pagination-input-div .el-input__inner {
    display: inline-block;
    height: 27px;
    line-height: 27px;
    text-align: center;
    text-align: -moz-center;
    text-align: -webkit-center;
  }

  .pagination-pager-div,
  .pagination-input-div {
    display: inline-block;
  }

  .pagination-input-div .pagination-input,
  /deep/ .pagination-input-div .el-input__inner {
    background-color: transparent;
    padding: 0;
    width: 40px;
    border-color: #2d8cf0;
    color: #FFFFFF;
  }

  .pagination-input-div {
    font-size: 12px;
  }

  .pagination-pager-div .current-page {
    color: #2d8cf0;
  }
</style>
